<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="static/css/bootstrap.min.css">
</head>


<body>
    <!-- 给一个容器 -->
    <div class="container">
        <div class="row text-center">
            <h1>学生信息管理系统</h1>
            <hr>

            <a href="/index.html" class="btn btn-success">首页</a>
            <a href="/add.html" class="btn btn-warning">添加</a>

            <br>
            <br>
        </div>


        <div class="row">

            <table id="tb" class="table table-bordered table-condensed table-hover table-responsive text-center">
                <!-- <tr>
                    <th>ID</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>操作</th>
                </tr> -->

                <!-- 将result的数据遍历出来 -->


                <!-- <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                    <td>
                        <a href="" class="btn btn-danger">删除</a>
                        <a href="" class="btn btn-warning">修改</a>
                    </td>
                </tr> -->










            </table>

        </div>
    </div>

</body>

</html>
<script src="./static/js/jquery.min.js"></script>
<script>
    $.ajax({
        url: "/index",
        type: "get",
        data: {},
        success: function (res) {
            console.log(res);
            let str = ""
            str += `<tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>年龄</th>
                        <th>操作</th>
                    </tr>`;
            res.data.forEach((item) => {
                str += `<tr>
                        <td>${item.id}</td>
                        <td>${item.name}</td>
                        <td>${item.sex}</td>
                        <td>${item.age}</td>
                        <td>${item.qqemail}</td>
                        <td>
                            <a  class="btn btn-danger"  did="${item.id}">删除</a>
                            <a href="/edit.html" class="btn btn-warning" cid="${item.id}">修改</a>
                        </td>
                    </tr>`
            })
            $('#tb').html(str)
        }, error: function (err) {
            console.log(err);
        }
    })
    //点击修改
    $('#tb').on('click', "a", function () {
        let id = $(this).attr('cid');
        // console.log(id);
        $.ajax({
            url: "/index",
            type: "post",
            data: { ids: id },
            success: function (res) {
                // console.log(res);
            }, error(err) {
                // console.log(err);
            }
        })
    })
    $('#tb').on('click', "a", function () {
        let id = $(this).attr('did');
        // console.log(id);
        $.ajax({
            url: "/del",
            type: "post",
            data: { ids: id },
            success: function (res) {
                location.href='/index.html'
            }, error(err) {
                console.log(err);
            }
        })
    })
</script>